package com.wpf.system.controller;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wpf.system.common.response.ResultUtils;
import com.wpf.system.common.response.ResultVO;
import com.wpf.system.entity.Feedback;
import com.wpf.system.entity.vo.FeedbackVO;
import com.wpf.system.entity.vo.PageVO;
import com.wpf.system.service.FeedbackService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/api/feedback")
public class FeedbackController {

    @Resource
    FeedbackService feedbackService;

    @GetMapping("/notProcessed")
    public Long statisticsTodayLogin(){
        return feedbackService.count(new LambdaQueryWrapper<Feedback>().isNull(Feedback::getGid));
    }

    @GetMapping
    public PageVO<FeedbackVO> getFeedbackList(
        @RequestParam(value = "uid",required=false) Long uid,
        @RequestParam(value = "nid",required=false) String nid,
        @RequestParam(value = "username",required=false) String username,
        @RequestParam(value = "title",required=false) String title,
        @RequestParam(value = "status",required=false) String status,
        @RequestParam(value = "startTime",required=false) String startTime,
        @RequestParam(value = "endTime",required=false) String endTime,
        @RequestParam(value = "page",required=false) int page,
        @RequestParam(value = "pageSize",required=false) int pageSize
    ){
        PageVO<FeedbackVO> feedbackVOPageVO = new PageVO<>();
        feedbackVOPageVO.setList(feedbackService.getFeedbackList(uid,nid,username,title,status,startTime,endTime,Math.max((page - 1) * pageSize,0),pageSize));
        feedbackVOPageVO.setTotal(feedbackService.getFeedbackList(uid,nid,username,title,status,startTime,endTime,null,null).size());
        return feedbackVOPageVO;
    }

    @PostMapping
    public ResultVO addFeedback(@RequestBody Feedback feedback){
        if (Convert.toBool(feedbackService.save(feedback))){
            return ResultUtils.success("已反馈成功！请耐心等候回复！");
        }else{
            return ResultUtils.error("反馈失败！请重试！");
        }
    }

    @PreAuthorize("hasAuthority('msg:feedback:list')")
    @PutMapping
    public ResultVO editFeedback(@RequestBody Feedback feedback){
        if (Convert.toBool(feedbackService.updateById(feedback))){
            return ResultUtils.success("已回复用户！");
        }else{
            return ResultUtils.error("回复失败！请重试！");
        }
    }

    @DeleteMapping("/{id}")
    public ResultVO deleteFeedback(@PathVariable("id") Long id){
        if (Convert.toBool(feedbackService.removeById(id))){
            return ResultUtils.success("已删除反馈！");
        }else{
            return ResultUtils.error("删除失败！请重试！");
        }
    }

    @DeleteMapping("/batch/delete/{ids}")
    public ResultVO deleteEquipmentBatch(@PathVariable("ids") String ids){
        List<Long> deleteIds = Convert.toList(Long.class,ids.split(","));
        if(feedbackService.removeBatchByIds(deleteIds)){
            return ResultUtils.success("批量删除反馈成功！");
        }
        return ResultUtils.error("批量删除反馈失败！");
    }

}
